package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.szgr.baoyin.R;
import com.szgr.baoyin.utils.MyTool;

public class FilterToolActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private EditText et_low;
    private EditText et_high;
    private RadioButton rb_sanling;
    private RadioButton rb_fanake;
    private RadioButton rb_ximenzi;
    private RadioButton rb_yinggui;
    private RadioButton rb_liang;
    private RadioButton rb_san;
    private RadioGroup rg_system;
    private RadioGroup rg_type;
    private int systemPos = 0;
    private int typePos = 0;
    private String[] sysStr = new String[]{"三菱","发那科","西门子"};
    private String[] xhStr = new String[]{"硬轨","线轨","线轨"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_filter_tool);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("筛选");
    }

    @Override
    public void initView() {
        et_low = (EditText) findViewById(R.id.filter_low_price);
        et_high = (EditText) findViewById(R.id.filter_high_price);
        RadioButton rb_lishi = (RadioButton) findViewById(R.id.store_left_btn);
        RadioButton rb_longmen = (RadioButton) findViewById(R.id.store_center_btn);
        RadioButton rb_woshi = (RadioButton) findViewById(R.id.store_right_btn);

        rb_sanling = (RadioButton) findViewById(R.id.system_01);
        rb_fanake = (RadioButton) findViewById(R.id.system_02);
        rb_ximenzi = (RadioButton) findViewById(R.id.system_03);

        rb_yinggui = (RadioButton) findViewById(R.id.type_01);
        rb_liang = (RadioButton) findViewById(R.id.type_02);
        rb_san = (RadioButton) findViewById(R.id.type_03);

        rg_system = (RadioGroup) findViewById(R.id.radio_group_system);
        rg_system.setOnCheckedChangeListener(this);
        rg_type = (RadioGroup) findViewById(R.id.radio_group_type);
        rg_type.setOnCheckedChangeListener(this);

        findViewById(R.id.sure_back).setOnClickListener(this);

        Intent intent = getIntent();
        if(intent.hasExtra("position")){
            int position = intent.getIntExtra("position", 0);
            switch (position){
                case 0:
                    rb_lishi.setChecked(true);
                    changeTextColor(rb_lishi, rb_longmen, rb_woshi);
                    break;
                case 1:
                    rb_longmen.setChecked(true);
                    changeTextColor(rb_longmen,rb_lishi , rb_woshi);
                    break;
                case 2:
                    rb_woshi.setChecked(true);
                    changeTextColor(rb_woshi, rb_longmen, rb_lishi);
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.sure_back:
                Intent intent = new Intent();
                intent.putExtra("low", MyTool.getText(et_low));
                intent.putExtra("high",MyTool.getText(et_high));
                String sys = "";
                for (int i = 0; i < rg_system.getChildCount(); i++) {
                    RadioButton childAt = (RadioButton) rg_system.getChildAt(i);
                    if(childAt.isChecked()){
                        sys = sysStr[i];
                    }
                }
                intent.putExtra("system", sys);
                String xh = "";
                for (int i = 0; i < rg_type.getChildCount(); i++) {
                    RadioButton childAt = (RadioButton) rg_type.getChildAt(i);
                    if(childAt.isChecked()){
                        xh = xhStr[i];
                    }
                }
                intent.putExtra("xinghao", xh);
                setResult(409, intent);
                finish();
                break;
        }
    }

    private void changeTextColor(RadioButton rb1, RadioButton rb2, RadioButton rb3) {
        rb1.setTextColor(0xffffffff);
        rb2.setTextColor(0xff234b64);
        rb3.setTextColor(0xff234b64);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int id = group.getId();
        switch (id){
            case R.id.radio_group_system:
                switch (checkedId){
                    case R.id.system_01:
                        changeTextColor(rb_sanling, rb_fanake, rb_ximenzi);
                        systemPos = 0;
                        break;
                    case R.id.system_02:
                        changeTextColor(rb_fanake, rb_sanling, rb_ximenzi);
                        systemPos = 1;
                        break;
                    case R.id.system_03:
                        changeTextColor(rb_ximenzi, rb_fanake, rb_sanling);
                        systemPos = 2;
                        break;
                }
                break;
            case R.id.radio_group_type:
                switch (checkedId){
                    case R.id.type_01:
                        changeTextColor(rb_yinggui, rb_liang, rb_san);
                        typePos = 0;
                        break;
                    case R.id.type_02:
                        changeTextColor(rb_liang, rb_yinggui, rb_san);
                        typePos = 1;
                        break;
                    case R.id.type_03:
                        changeTextColor(rb_san, rb_liang, rb_yinggui);
                        typePos = 2;
                        break;
                }
                break;
        }
    }
}
